/**
 * @author 徐楠
 * @date 2022/5/8 22:35
 * @version 1.0
 */

package com.xunan.likou;

import java.util.*;

public class CompressedString {
    public static void main(String[] args) {
        char[] chars = {'a', 'b', 'b', 'c', 'c', 'c'};
        //int i = compress2(chars);
        //System.out.println(i);
        int i1 = compress3(chars);
        System.out.println(i1);
    }

    public static int compress1(char[] chars) {
        Map<Character, Integer> map = new HashMap<>();
        for (char aChar : chars) {
            map.put(aChar, map.getOrDefault(aChar, 0) + 1);
        }
        for (int i = 0; i < chars.length; i++) {
            map.put(chars[i], map.getOrDefault(chars[i], 0) + 1);
            //chars[i] = '';
        }
        List<String> list = new ArrayList<>();

        Set<Map.Entry<Character, Integer>> entries = map.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {
            if (entry.getValue() == 1) {
                list.add(entry.getKey() + "");
            } else {
                list.add(entry.getKey() + "");
                list.add(entry.getValue() + "");
            }
        }
        System.out.println(list);
        return list.size();
    }

    public static int compress2(char[] chars) {
        int n = chars.length;
        int i = 0;
        int j = 0;
        while (i < n) {
            int index = i;
            while (index < n && chars[index] == chars[i]) {
                index++;
            }
            //因为上面index++了，所以count不需要再+1
            int count = index - i;
            chars[j++] = chars[i];
            if (count > 1) {
                int start = j;
                int end = start;
                //如果组长度为 10 或 10 以上，则在 chars 数组中会被拆分为多个字符。
                while (count != 0) {

                    chars[end++] = (char) ((count % 10) + '0');
                    count /= 10;
                }
                reverse(chars, start, end - 1);
                j = end;
            }
            i = index;
            System.out.print(j + " ");
        }
        System.out.println();

        for (char aChar : chars) {
            System.out.print(aChar + " ");
        }
        System.out.println();

        return j;
    }

    public static void reverse(char[] cs, int start, int end) {
        while (start < end) {
            char t = cs[start];
            cs[start] = cs[end];
            cs[end] = t;
            start++;
            end--;
        }
    }

    public static int compress3(char[] chars) {
        int n = chars.length;
        int i = 0;
        int j = 0;
        while (i < n) {
            int index = i;
            while (index < n && chars[index] == chars[i]) {
                index++;
            }
            //因为上面index++了，所以count不需要再+1
            int count = index - i;
            chars[j++] = chars[i];
            if (count > 1) {
                int start = j;
                int end = start;
                //如果组长度为 10 或 10 以上，则在 chars 数组中会被拆分为多个字符。
                String s = count + "";
                char[] sToChars = s.toCharArray();
                for (char sToChar : sToChars) {
                    chars[end++] = sToChar;
                }
                j = end;
                System.out.print(j + " ");
            }

            i = index;
        }

        System.out.println();
        for (char aChar : chars) {
            System.out.print(aChar + " ");
        }
        System.out.println();

        return j;
    }

}
